// pages/mySubordinate/mySubordinate.js

const DB = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // mySubordinateList: []
    arrlist: [],
    kefu_openid:"odRMY0Ts6AT_0XYd5xHkCFWqbxu0",
  },
  news(e) {
    getApp().sendMsg()
    console.log(e)
    let item = e.currentTarget.dataset.item
    wx.setStorageSync('chat_item', item)
    wx.navigateTo({
      url: '/pages/chat/chat'
      // url: '/pages/chat/chat?item=' + JSON.stringify(item) // 这里是要跳转的路径
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {

    let that = this
    
    this.setData({
      my_img:wx.getStorageSync('userInfo').userInfoDetail.hande,
      my_name:wx.getStorageSync('userInfo').userInfoDetail.nicheng,
    })
    const _ = DB.command;
    var _openid = wx.getStorageSync('userInfo')._openid;

    // 是客服，显示所有的列表
    if(_openid==this.data.kefu_openid){
      console.log('是客服，显示所有的列表',_openid)
      DB.collection('friends').get().then(res=>{
        
        that.setData({
          arrlist: res.data,_openid
        })
      })
    }else{

      DB.collection('friends').where(_.or([{
        a: _.eq(_openid)
      },
      {
        b:_.eq(_openid)
      }
    ]))
    .get({
      success: function (res) {
        console.log('——————————————————————————>', res.data)
        let list = []
        res.data.forEach(item=>{
          if(item.a == _openid&&item.a_hongdian==true){
            console.log('我是A')
            item.hongdian = true
          }
          if(item.b == _openid&&item.b_hongdian==true){
            console.log('我是B')

            item.hongdian = true
          }
          if(!item.guanbiChat){
            list.push(item)
          }
        })
        that.setData({
          arrlist: list,_openid
        })

      }
    })
    }
  },
  loadData: function (options) {
    var self = this;
    getApp().request({
      url: getApp().api.user.isa,
      success: function (res) {
        var list = res.data.split('---');
        var arr = list.map(item => {
          var iitem = JSON.parse(item);
          return iitem
        })
        console.log('list11', arr)

        self.setData({
          mySubordinateList: arr
        })
      }
    });
  },

})

function js_date_time(unixtime) {
  var dateTime = new Date(parseInt(unixtime))
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1;
  var day = dateTime.getDate();
  var hour = dateTime.getHours();
  var minute = dateTime.getMinutes();
  var second = dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString()); //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
  return timeSpanStr;

}